package cmd

import (
	"fmt"
	"github.com/spf13/cobra"
	"wdhcr-jvm-cli/utils"
)

var uninstallCmd = &cobra.Command{
	Use:   "uninstall",
	Short: "Uninstall switchable version",
	Args:  cobra.MinimumNArgs(1),
	Run: func(cmd *cobra.Command, args []string) {
		option := args[0]
		uninstall(option)
	},
}

func init() {
	rootCmd.AddCommand(uninstallCmd)
}

func uninstall(option string) {
	version := utils.GetDefaultJdkVersion()
	if option == version {
		fmt.Println("The default version cannot be uninstalled")
		return
	}
	homeDir := utils.GetUserHome()
	_, err := utils.ExecSh("rm -rf " + homeDir + "/.wjvm/jdk_package/" + option + ".dmg")
	if err != nil {
		return
	}
	fmt.Println("Uninstall " + option + " successfully!")

}
